package com.base.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.ByteArray;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.utils.NameUtil;
	import mx.utils.ObjectUtil;
	
	/**
	 * 工具包 
	 * @author Administrator
	 */	
	public class Utils
	{
		public function Utils()
		{
			throw new Error("Don't need instance");
		}
		
		/**
		 * @param arr 遍历的数组 
		 * @param data 查找的数据
		 * @return 查找项
		 */		
		public static function findDataByArray( arr:ArrayCollection , data:Object ):Object {
			for each( var item:Object in arr ) {
				if( item["data"] == data ) return item;
			}
			return null;
		}
		
		public static function isSimple( type:String ):Boolean {
			return type == "String" || type == "Number" ||  type == "Boolean" || type == "uint" || type == "int" || type == "Date" ;
		}
		
		/**
		 * 将ui层数据转换成数据层 ui控件名等于数据层属性名 
		 * @param ui 
		 * @param bean
		 * @return 
		 */				
		public static function convertToBean( ui:DisplayObjectContainer , bean:Object ):Object {
			var objInfo:XML = describeType( bean );
			
			for each(var q:XML in objInfo.accessor){
				
				 if( isSimple( String(q.@type) ) ) {
					
					var child:DisplayObject = getControlById( ui , q.@name );
					if( !child ) continue;
					bean[ q.@name ] = getControlValue( child );
					
				} else {
					
					var className:String = String(q.@type).replace("::",".");
					var cls:Class = getDefinitionByName( className ) as Class;
					var newParser:* = new cls();
					bean[ q.@name ] = convertToBean( ui , newParser );  
					
				}
				
			} 
			
			
			return bean;
		}
		
		/**
		 * 获取控件的值 
		 * @param ui
		 * @return 
		 */		
		public static function getControlValue( ui:DisplayObject ):Object {
			var className:String = NameUtil.getUnqualifiedClassName( ui );
			if( className == "TextInput" ) {
				
				return ui["text"];
				
			} else if( className == "TextArea" ) {
				
				return ui["text"];
				
			} else if( className == "NComboBox" ) {
				
				return ui["selectedItemByData"];
				
			} else if( className == "CheckBox") {
				
				return ui["selected"];
				
			} else if( className == "TimeField" ) {
				return ui["selectedDate"];
			}
			
			return null;
		}
		
		/**
		 * 将数据层数据转换成ui层 ui控件名等于数据层属性名 
		 * @param ui 
		 * @param bean
		 * @return 
		 */				
		public static function convertToUI( ui:DisplayObjectContainer , bean:Object ):void {
			var objInfo:XML = describeType( bean );
			
			for each(var q:XML in objInfo.accessor){
				
				if(  isSimple( String(q.@type) ) ) {
					
					var child:DisplayObject = getControlById( ui , q.@name );
					setControlValue( child , bean[ q.@name ]);
					
				} else {
					convertToUI( ui , bean[ q.@name ] );  
				}
			} 
		}
		
		/**
		 * 设置控件的值
		 * @param ui
		 * @param val
		 */		
		public static function setControlValue( ui:DisplayObject , val:* ):void {
			var className:String = NameUtil.getUnqualifiedClassName( ui );
			if( className == "TextInput" ) {
				
				ui["text"] = val;
				
			} else if( className == "TextArea" ) {
				
				ui["text"] = val;
				
			} else if( className == "NComboBox" ) {
				
				ui["selectedItemByData"] = val;
				
			} else if( className == "CheckBox") {
				
				ui["selected"] = val;
				
			}  else if( className == "TimeField" ) {
				
				 ui["selectedDate"] = val;
				 
			}
			
		}
		
		/**
		 * 获取组建id 
		 * @param container
		 * @param id
		 * @return 
		 */		
		public static function getControlById( container:DisplayObject,id:String ):DisplayObject {
			try {
				
				return container[id];
				
			} catch(error:Error)  {
				return null;
			}
			return null;
		}
		
		/**
		 * 对象拷贝
		 * @param sourceObject
		 * @param destinationObject
		 */		
		public static function copyObject(sourceObject:Object, destinationObject:Object):Object {
			
			var objInfo:XML = describeType( destinationObject );
			
			for each(var q:XML in objInfo.accessor){
				if( !sourceObject.hasOwnProperty(q.@name) ) continue;
				if(  isSimple( String(q.@type) ) ) {
					
				
					destinationObject[ q.@name ] = sourceObject[ q.@name ];
					
				} else {
					
					var className:String = String(q.@type).replace("::",".");
					var cls:Class = getDefinitionByName( className ) as Class;
					var newParser:* = new cls();
					destinationObject[ q.@name ] = copyObject( sourceObject[ q.@name ] , newParser );  
					
				}
				
			}
			return destinationObject;
			
		}
		
		/**
		 * 时间是否相等 
		 * @param d
		 * @param d1
		 * @return 
		 */		
		public static function dateEquals( d:Date , d1:Date ):Boolean {
			return d && d1 && d.hours == d1.hours && d.minutes == d1.minutes; 
		}
		
		/**
		 * 
		 * @param d
		 * @return 
		 */		
		public static function dateToString( d:Date ):String {
			return dateFormat( d , "YYYY-MM-DD HH:NN:SS");
		}
		
		/**
		 * 
		 * @param d
		 * @return 
		 */		
		public static function dateFormat( d:Date ,formatString:String ):String {
			var format:DateFormatter = new DateFormatter();
			format.formatString = formatString;
			return format.format( d );
		}
		
		/**
		 * 保留中文
		 * @param context
		 * @return 
		 */		
		public static function text_zh(context:String):String {
			return context.replace(/[^\u4E00-\u9FA5]/g,"");
		}
	}
}